package com.hzcx.app.simplechat.ui.friend.adapter;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendLableListBean;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import java.util.ArrayList;
import java.util.List;

public class FriendAdapter extends BaseQuickAdapter<FriendBean, BaseViewHolder> {

    private OnFriendListItemOnClickListener listener;

    private int tag;

    public FriendAdapter(@Nullable List<FriendBean> data) {
        super(R.layout.rv_item_friend_header, data);
    }

    public FriendAdapter(@Nullable List<FriendBean> data, int tag) {
        super(R.layout.rv_item_friend_header, data);
        this.tag = tag;
    }

    @Override
    protected void convert(BaseViewHolder helper, FriendBean item) {

        TextView tvHeader = helper.getView(R.id.tv_header);
        RecyclerView rvFriendContent = helper.getView(R.id.rv_friend_content);

        List<FriendCityBean> list = new ArrayList<>();
        List<FriendLableListBean> lableList = new ArrayList<>();
        FriendContentAdapter contentAdapter = new FriendContentAdapter(list, tag);
        FriendLableListAdapter lableListAdapter = new FriendLableListAdapter(lableList, false);
        rvFriendContent.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        if (item != null && item.getLableList() != null) {
            rvFriendContent.setAdapter(lableListAdapter);

            lableListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                switch (view.getId()){
                    case R.id.cons_content:
                        if (listener != null) {
                            listener.itemOnClick(helper.getAdapterPosition(), position, 0, null);
                        }
                        break;
                }
            });
        } else {
            rvFriendContent.setAdapter(contentAdapter);
            contentAdapter.setOnItemClickListener((adapter, view, position) -> {
                if (listener != null) {
                    listener.itemOnClick(helper.getAdapterPosition(), position, list.get(position).getMember_id(), list.get(position));
                }
            });
        }

        if (item == null || EmptyUtils.isEmpty(item.getName()) || item.getCity() == null || item.getCity().size() <= 0) {
            if (item != null && item.getLableList() != null) {
                tvHeader.setVisibility(View.VISIBLE);
                rvFriendContent.setVisibility(View.VISIBLE);
                tvHeader.setText(item.getName());
                lableList.addAll(item.getLableList());
                lableListAdapter.notifyDataSetChanged();
            } else {
                tvHeader.setVisibility(View.GONE);
                rvFriendContent.setVisibility(View.GONE);
            }
        } else {
            tvHeader.setVisibility(View.VISIBLE);
            rvFriendContent.setVisibility(View.VISIBLE);
            tvHeader.setText(item.getName());
            if (item != null && item.getLableList() != null) {
                lableList.addAll(item.getLableList());
                lableListAdapter.notifyDataSetChanged();
            } else {
                list.addAll(item.getCity());
                contentAdapter.notifyDataSetChanged();
            }
        }

    }

    public interface OnFriendListItemOnClickListener {
        void itemOnClick(int headerPos, int contentPos, int id, FriendCityBean bean);
    }

    public void setOnFriendListItemOnClickListener(OnFriendListItemOnClickListener listener) {
        this.listener = listener;
    }
}
